#include<bits/stdc++.h>
using namespace std;
int n,T,m[1000000],ans;
void dfs(int x,int a,int b,int c,int s){
    if(x==T-1){ans=max(ans,s);return ;}
    if(c){
        dfs(x+1,x,b,1,s+(m[x+1]==m[x])*m[x]);
        if(b==-1) dfs(x+1,x,b,0,s);
        else dfs(x+1,x,b,0,s+(m[x+1]==m[b])*m[b]);
    }
    else{
        dfs(x+1,a,x,0,s+(m[x+1]==m[x])*m[x]);
        if(a==-1) dfs(x+1,a,x,1,s);
        else dfs(x+1,a,x,1,s+(m[x+1]==m[a])*m[a]);
    }
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&n);
    while(n--){
        ans=0;
        scanf("%d",&T);
        for(int i=0;i<T;i++) scanf("%d",&m[i]);
        if(T>27) printf("%d\n",T-1);
        else{dfs(0,-1,-1,1,0);printf("%d\n",ans);}
    }
    return 0;
}
